package com.easepal802s.project.ui.presenter;


import com.clj.fastble.data.BleDevice;
import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.ble.HexUtils;
import com.easepal802s.project.observer.BleDataObserver;
import com.easepal802s.project.ui.iview.IFraAirView;
import com.ogawa.base.utils.CommonUtil;

/**
 * Created by FHB on 2018/4/1.
 */

public class FraAirPresenter implements BleDataObserver.OnBleListerner{
    IFraAirView mView;
    private byte mByteAirStrong;//强度
    private byte mByteAirPosition;//位置
    private int mAirStrong = 0;
    public FraAirPresenter(IFraAirView view) {
        mView = view;
        BleDataObserver.getInst().registerBleDataObserver(this);
    }
    public void OnDestory(){
        BleDataObserver.getInst().removeBleDataObserver(this);
    }


    @Override
    public void onGetData(String data) {
        if (data.startsWith(BleConstant.BAG2_HEAD)) {
            byte[] buffer = HexUtils.hexToByte(data);
            byte byteMin =buffer[17];
            byte byteSec =buffer[18];
            byte byte7 =buffer[7];
            mView.getTime(byteMin,byteSec);
            if(mByteAirPosition !=byte7){
                mByteAirPosition = byte7;
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,7,0))){
                    mView.getAirPosition(0,1);
                }
                else{
                    mView.getAirPosition(0,0);
                }
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,7,1))){

                    mView.getAirPosition(1,1);
                }
                else {
                    mView.getAirPosition(1,0);
                }
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,7,2))){
                    mView.getAirPosition(2,1);
                }
                else {
                    mView.getAirPosition(2,0);
                }
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,7,3))){
                    mView.getAirPosition(3,1);
                }
                else {
                    mView.getAirPosition(3,0);
                }
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,7,4))){
                    mView.getAirPosition(4,1);
                }
                else {
                    mView.getAirPosition(4,0);
                }
            }
        }
        if (data.startsWith(BleConstant.BAG1_HEAD)) {
            byte[] buffer = HexUtils.hexToByte(data);
            byte byte11 =buffer[11];
            if(mByteAirStrong !=byte11){
                mByteAirStrong = byte11;

                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,5))){
                    mAirStrong=5;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,4))){
                    mAirStrong=4;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,3))){
                    mAirStrong=3;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,2))){
                    mAirStrong=2;
                }
                else  if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,1))){
                    mAirStrong=1;
                }

                else {
                    mAirStrong=0;
                }
                mView.getAirStrong(mAirStrong);

            }
        }
    }
    @Override
    public void onConnectState(int state) {

    }

    @Override
    public void onTargetDeviceFind(BleDevice bleDevice) {

    }

    @Override
    public void onStarScan() {

    }

    @Override
    public void onNoDeviceFind() {

    }
    @Override
    public void onPowerOff() {

    }

    @Override
    public void onDataChange(String bage, int auto, int skill, int air, String name, String commond) {

    }

    @Override
    public void onShoulderAdjust(int data) {

    }
}
